package com.yuncommunity.runcircle.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.oldfeel.utils.DensityUtil;
import com.yuncommunity.runcircle.R;
import com.yuncommunity.runcircle.item.LocationItem;

/**
 * Created by oldfeel on 15-12-1.
 */
public class RunDataView extends View {
    LocationItem[] datas;
    private double averageDistance;
    private double maxDistance;
    private double minDistance;
    private Paint linePaint;
    private Paint effectsPaint;
    private Bitmap bitmap;
    private int bitmapWidth;
    private int bitmapHeight;

    public RunDataView(Context context) {
        super(context);
        init();
    }

    public RunDataView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RunDataView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        linePaint = new Paint();
        linePaint.setColor(Color.WHITE);
        linePaint.setTextSize(3);

        effectsPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        effectsPaint.setStyle(Paint.Style.STROKE);
        effectsPaint.setColor(Color.GRAY);
        effectsPaint.setStrokeWidth(1);
        effectsPaint.setPathEffect(new DashPathEffect(new float[]{1, 2, 4, 8}, 1));

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_circle_green);
        bitmapWidth = bitmap.getWidth();
        bitmapHeight = bitmap.getHeight();
    }

    public void setData(LocationItem[] datas, double averageDistance, double minDistance, double maxDistance) {
        this.datas = datas;
        this.averageDistance = averageDistance;
        this.minDistance = minDistance;
        this.maxDistance = maxDistance;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (datas == null) {
            return;
        }
        int height = getHeight() - DensityUtil.dip2px(getContext(), 20);
        int dp10 = DensityUtil.dip2px(getContext(), 10);
        int width = getWidth() - DensityUtil.dip2px(getContext(), 10);
        float lastX = -1, lastY = -1; // 上一个点的坐标
        for (int i = 0; i < datas.length; i++) { // 绘制连线
            LocationItem item = datas[i];
            float x = (float) (width / 7 * (i + 0.5)) + dp10;
            float y = (height - (float) (height / (maxDistance - minDistance) * (item.distance - minDistance)) + dp10);
            if (lastX != -1 && lastY != -1) {
                canvas.drawLine(x, y, lastX, lastY, linePaint);
            }
            lastX = x;
            lastY = y;
        }
        for (int i = 0; i < datas.length; i++) { // 绘制圆球
            LocationItem item = datas[i];
            float x = (float) (width / 7 * (i + 0.5)) + dp10;
            float y = (height - (float) (height / (maxDistance - minDistance) * (item.distance - minDistance)) + dp10);
            canvas.drawBitmap(bitmap, x - (bitmapWidth / 2), y - (bitmapHeight / 2), null);
        }
        float averageY = (height - (float) (height / (maxDistance - minDistance) * (averageDistance - minDistance)) + dp10);
        canvas.drawLine(0, 0, getWidth(), 0, effectsPaint); // 绘制顶部线
        canvas.drawLine(0, averageY, getWidth(), averageY, effectsPaint); // 绘制平均线
        canvas.drawLine(0, getHeight(), getWidth(), getHeight(), effectsPaint); // 绘制底部的线
    }
}
